<?php
class Control_Gm_sendMulUserAward {
	function sendMulUserAward() {
		$game 		= MooForm::request('game');
		$server 	= MooForm::request('serverUrl');
        $dbAwardMsg = $awardMsg 	= MooForm::request('awardMsg');
        $dbAwardInfo = $awardInfo	= MooForm::request('awardInfo');
		$getPlayerIds	= MooForm::request('playerIds'); 
		
		$playerIds = explode(",", $getPlayerIds);
		
		$awardMsg = urlencode($awardMsg);
		$awardInfo = urlencode($awardInfo);
		
		// 后台用户名
		$userName = MooObj::get('User')->getUserName();
		
		$params = array();
		
		if ($awardMsg) {
			$params['message'] = $awardMsg;
		}
		if ($awardInfo) {
			$params['reward'] = $awardInfo;
		}
		
		$serverLists = MooObj::get('Gm')->getServerListByGame($game);
		$cmd = "playerreward";
		
		$allRes = "";
		foreach ($playerIds as $key => $playerId) {
			$params['playerid'] = $playerId;
			$serverUrl = $serverLists[$server]['url'];
			$url = MooObj::get('Gm')->getUrl($serverUrl, $cmd, $userName, $params);
			$res = MooUtil::curl_send($url);
			if($res === false) {
				$msg = "false";  // 连接服务器失败
                $currentData['send_result'] = '连接服务器失败';
			} else {
				$returnData = MooJson::decode($res, true);
				if($returnData['status'] == 1) {
					$msg = "success";
					$currentData['send_result'] = '发送成功';
				} else {
					$msg = "fail";
                    $currentData['send_result'] = '发送失败';
				}
			}

            $currentData['game'] = $game;
            $currentData['servers'] = $server;
            $currentData['award_msg'] = $dbAwardMsg;
            $currentData['award_info'] = $dbAwardInfo;
            $currentData['playerid'] = $playerId;
            $currentData['send_name'] = $userName;
            $currentData['send_time'] = time();
            $currentData['send_type'] = '玩家邮件';

			if(!$allRes) {
				$allRes = "url:" . $url . "--result:" . $msg . "\n";
			} else {
				$allRes =  "url:" . $url . "--result:" . $msg . "\n" . $allRes;
			}
            /*记录日志*/
            $awardLogDao =  MooDao::get('AwardLog');
            // 插入
            $awardLogDao->setData('game', $currentData['game']);
            $awardLogDao->setData('servers', $currentData['servers']);
            $awardLogDao->setData('award_msg', $currentData['award_msg']);
            $awardLogDao->setData('award_info', $currentData['award_info']);
            $awardLogDao->setData('playerid', $currentData['playerid']);
            $awardLogDao->setData('send_name', $currentData['send_name']);
            $awardLogDao->setData('send_type', $currentData['send_type']);
            $awardLogDao->setData('send_result', $currentData['send_result']);
            $awardLogDao->setData('send_time', time());

            if(!$awardLogDao->insert()) {
                $allRes .= '数据库日志添加失败';
            }
		}
		
		$date = date("Y-m-d");
		$log = ROOT_PATH . "/urlLogs/" . "sendMulUserAward.log." . $date;
		MooFile::write($log, date('Y-m-d H:i:s') ."--\n" . $allRes."\n", true);
		exit(urldecode($allRes));
	}
}